<?php
namespace app\model;
use think\Model;

class Authors extends Model
{
    public function profile(){
        return $this->hasOne(books_category::class,'cat_id','authorkind');
    }
    public function kind(){
        return $this->hasMany(books_category::class,'cat_id','authorkind');
    }
    public function searchCountryAttr($query,$value){
        if($value){
            $query->where('country',$value);
        }
//        $query->where('country', 'like', $value.'%');
//        if (isset($data['sort'])) {
//            $query->order($data['sort']);
//        }
    }
    public function searchAuthornameAttr($query,$value){
//        return $value?$query->where('author_name','like','%'.$value.'%'):'';
        if($value){
            $query->where('authorname','like','%'.$value.'%');
        }
    }
    public function searchAuthorkindAttr($query,$value){
////        return $value?$query->where('author_name','like','%'.$value.'%'):'';
        $valu=$this->hasWhere('profile', ['cat_name'=>$value],'authorkind')->find();
        $valu=json_decode($valu,true);
        if($valu){
            $valu=$valu['authorkind'];
            $query->where('authorkind',$valu);
        }

    }
}